
const state = {
    // 是否收起
    isCollapse: false,
    selectMenu: [],
    routerList: [],
    // 默认菜单高亮
    menuActive: "1-1"
};
const mutations = {
    // 菜单展开/收起 开关控制
    collapseMenu(state) {
        state.isCollapse =!state.isCollapse
    },
    // tab菜单控制
    addMenu(state, payload) {
        // 对数据进行去重,不存在则加入数据
       if( state.selectMenu.findIndex( item => item.path == payload.path) === -1){
           state.selectMenu.push(payload)
       }
    },
    closeMenu(state, payload){
        // 找到点击的索引
        const index = state.selectMenu.findIndex( val => val.name === payload.name);
        // 通过索引删除数组指定元素
        state.selectMenu.splice(index,1);
    },
    // 左侧动态路由菜单控制
    dynamicMenu(state, payload){
        //console.log('payload', payload);
        // 通过glob 导入
        const modules = import.meta.glob('../views/**/*.vue')
        //console.log(modules);

        function routerSet(router) {
            router.forEach(route => {
                if(!route.children){
                    //console.log('meta: ',route.metaData);
                    const url = `../views${route.meta.path}/index.vue`;
                    // 拿到获取的vue组件
                    route.component = modules[url];
                } else {
                    routerSet(route.children)
                }
            })
            // 判断子菜单 拼接路由数据

        }
        routerSet(payload);
        //console.log('payload',payload);
        // 拿到完整的路由数据
        state.routerList = payload;
    },
    updateMenuActive(state, payload){
        state.menuActive = payload;
    }
};

export default {
    state,
    mutations
}
